import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;

import shapes.Circle;
import shapes.Square;
import shapes.Triangle;
import trajectories.LineTrajectorie;
import trajectories.SquareTrajectorie;
import trajectories.Trajectory;

public class Launcher implements ActionListener {

    private static final Object[] COLORS = { "BLACK", "BLUE", "GRAY", "ORANGE" };
    private static final Object[] TRAJECTORIES = { "Square", "Line" };

    private JList listOfColors;
    private JList listOfTrajectories;

    private JTextField textFieldX;
    private JTextField textFieldY;

    private JLabel exceptions;
    private JLabel coordinateY;
    private JLabel coordinateX;
    private JLabel trajectoriesLabel;
    private JLabel color;

    private JButton clear;
    private JButton square;
    private JButton triangle;
    private JButton circle;

    private JScrollPane jspTraectories;
    private JScrollPane jspColors;

    private Field field;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Launcher launcher = new Launcher();
                launcher.showMenu();
                launcher.field.moveShapes();
            }
        });
    }

    private void showMenu() {

        JFrame jf = new JFrame("Task");
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        field = new Field();
        field.setLayout(null);

        circle = new JButton("Circle");
        circle.setBounds(5, 5, 90, 20);
        circle.setActionCommand("Circle");
        circle.addActionListener(this);

        field.add(circle);

        triangle = new JButton("Triangle");
        triangle.setBounds(5, 30, 90, 20);
        triangle.setActionCommand("Triangle");
        triangle.addActionListener(this);

        field.add(triangle);

        square = new JButton("Square");
        square.setBounds(5, 55, 90, 20);
        square.setActionCommand("Square");
        square.addActionListener(this);

        field.add(square);

        clear = new JButton("Clear");
        clear.setBounds(340, 350, 90, 20);
        clear.setActionCommand("Clear");
        clear.addActionListener(this);

        field.add(clear);

        color = new JLabel("Colors");
        color.setBounds(5, 80, 90, 20);
        field.add(color);

        trajectoriesLabel = new JLabel("Trajectories");
        trajectoriesLabel.setBounds(5, 160, 90, 20);
        field.add(trajectoriesLabel);

        listOfColors = new JList(COLORS);
        listOfColors.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        jspColors = new JScrollPane(listOfColors);
        jspColors.setBounds(5, 105, 90, 50);
        field.add(jspColors);

        listOfTrajectories = new JList(TRAJECTORIES);
        listOfTrajectories.setBounds(5, 155, 90, 20);

        jspTraectories = new JScrollPane(listOfTrajectories);
        jspTraectories.setBounds(5, 185, 90, 50);
        field.add(jspTraectories);

        coordinateX = new JLabel("Coordinate X");
        coordinateX.setBounds(5, 240, 90, 20);
        field.add(coordinateX);

        textFieldX = new JTextField();
        textFieldX.setBounds(5, 270, 90, 20);
        field.add(textFieldX);

        coordinateY = new JLabel("Coordinate Y");
        coordinateY.setBounds(5, 295, 90, 20);
        field.add(coordinateY);

        textFieldY = new JTextField();
        textFieldY.setBounds(5, 320, 90, 20);
        field.add(textFieldY);

        exceptions = new JLabel();
        exceptions.setBounds(5, 345, 300, 20);
        exceptions.setVisible(false);
        exceptions.setForeground(Color.red);
        field.add(exceptions);

        jf.add(field);
        jf.pack();
        jf.setVisible(true);
        jf.setResizable(false);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            exceptions.setVisible(false);
            int colorIndex = listOfColors.getSelectedIndex();
            int trajectoriesIndex = listOfTrajectories.getSelectedIndex();
            int coordinateX = Integer.parseInt(textFieldX.getText()) + 100;
            int coordinateY = Integer.parseInt(textFieldY.getText()) + 5;
            if (((coordinateX + 30) > 440) || ((coordinateY + 30) > 340) || (coordinateX < 0) || (coordinateY < 0)) {
                throw new NumberFormatException();
            }
            Color color = null;
            Trajectory trajectory = null;
            switch (colorIndex) {
                case 0:
                    color = Color.BLACK;
                    break;
                case 1:
                    color = Color.BLUE;
                    break;
                case 2:
                    color = Color.GRAY;
                    break;
                case 3:
                    color = Color.ORANGE;
                    break;
            }
            switch (trajectoriesIndex) {
                case 0:
                    trajectory = new SquareTrajectorie();
                    break;
                case 1:
                    trajectory = new LineTrajectorie();
                    break;
            }
            if (e.getActionCommand().equals("Circle")) {
                field.add(new Circle(coordinateX, coordinateY, color, trajectory));
            } else if (e.getActionCommand().equals("Triangle")) {
                field.add(new Triangle(coordinateX, coordinateY, color, trajectory));
            } else if (e.getActionCommand().equals("Square")) {
                field.add(new Square(coordinateX, coordinateY, color, trajectory));
            } else if (e.getActionCommand().equals("Clear")) {
                field.clear();
            }
        } catch (NumberFormatException nfe) {
            exceptions.setText("\"x\" must be from 0 to 300, \"y\" must be from 0 to 300");
            exceptions.setVisible(true);
        }

    }

}
